/*
 *------------------------------------------------------------------------------
 * list.h - definitions for the doubly-linked list library
 *
 *
 *
 *------------------------------------------------------------------------------
 */

#ifndef LIST_H
#define LIST_H


#define LIST_FOR_ALL_NODES(lst, n) \
    for(n = lst->head; n != NULL; n = n->next)

/*
 * Intrusive structure that doubly links together elements of a user list. This
 * structure MUST be the first field of a user list node structure.
 */
typedef struct node_t {
    struct node_t *next;
    struct node_t *prev;
} node;


/*
 *
 */
typedef struct list_t {
    node *head;
    node *tail;
    int   count;
} list;


/*
 *
 */
typedef int (*list_compare_fn)(void *m, void *n);


/*
 *
 */
typedef void (*list_free_fn)(void *n);


/*
 *
 */
enum {
    LIST_EOK   =  0,
    LIST_ENULL = -1,
    LIST_ESIZE = -2,
    LIST_EMAX  = -3
};


/*
 *
 */
list *list_new();
int list_free(list *lst, list_free_fn nfree);
int list_delink(list *lst, void *t);
int list_append(list *lst, void *t);
int list_prepend(list *lst, void *t);
void list_random_insert(list *lst, void *n);
void list_sort(list *lst, list_compare_fn comp);
void list_sorted_insert(list *lst, list_compare_fn comp);


#endif


